package cn.edu.bit.lixin.blog.util;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.edu.bit.lixin.blog.entity.Blog;
import cn.edu.bit.lixin.blog.entity.Category;
import cn.edu.bit.lixin.blog.entity.Reply;
import cn.edu.bit.lixin.blog.entity.User;

public class AddItem {

	public static void main(String[] args) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		//添加分类、用户以及日志
	/*	Category cate = setCategory();
		User user = setUser();
		Blog blog = setBlog();
		
		blog.setCategory(cate);
		blog.setUser(user);
		
		session.save(cate);
		session.save(user);
		session.save(blog);
		session.getTransaction().commit();*/
		
		
		
		//添加日志
		String hql = "from Category where id=1";
		Query query = session.createQuery(hql);
		List<Category> cates = query.list();
		Category cate = cates.get(0);
		
		String hql1 = "from User where id=1";
		Query query1 = session.createQuery(hql1);
		List<User> users = query1.list();
		User user = users.get(0);
		
		
		Blog blog = setBlog();
		blog.setCategory(cate);
		blog.setUser(user);
	
		session.save(blog);
		session.getTransaction().commit();
		
		
		//添加回复
		/*String hql = "from Blog where id=3";
		Query query = session.createQuery(hql);
		List<Blog> blogs = query.list();
		Blog blog = blogs.get(0);
		
		Reply reply = setReply();
		reply.setBlog(blog);
		session.save(reply);
		session.getTransaction().commit();*/
		
		
	}
	
	public static Category setCategory(){
		Category cate = new Category();
		cate.setCatename("生活");
		cate.setCateDesc("一些生活上的琐事");
		return cate;
	}
	
	public static Blog setBlog(){
		Blog blog = new Blog();
		blog.setBlogname("第6篇日志");
		Date date = new Date();
		blog.setPubTime(date);
		blog.setContent("这是第6篇日志");
		return blog;
	}

	public static User setUser(){
		User user = new User();
		user.setUsername("admin");
		user.setPassword("111111");
		Date date = new Date();
		user.setRegTime(date);
		user.setUserGroup(0);
		user.setEmail("lixin@lixin.com");
		return user;
	}
	
	public static Reply setReply(){
		Reply reply = new Reply();
		reply.setProperty(0);
		Date date = new Date();
		reply.setPubTime(date);
		reply.setReplierId(-1);
		reply.setReplierName("xxx");
		reply.setContent("这是一个回复");
		return reply;
	}
}
